package be.phl.obstacleapp;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import be.phl.layoutAdapter.ItemProblemRow;
import be.phl.layoutAdapter.SingleItemProblemRowAdapter;
import be.phl.model.DBAdapterScreening;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class ScreeningProblemFragment extends ListFragment {

	private static final String TAG = "SrceeningProblemFragment";
	private ArrayList<ItemProblemRow> datalist;
	List<ItemProblemRow> list;

	/**
	 * key for arguments
	 */
	public static final String ARG_SCREENINGITEM_ID = "ScreeningItemId";

	/**
	 * List with the roomitems
	 */
	private static int ScreeningItemId;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		ScreeningItemId = getArguments().getInt(ARG_SCREENINGITEM_ID);
		Log.i(TAG, "ScreeningItemId: " + ScreeningItemId);

		datalist = new ArrayList<ItemProblemRow>();

		// get all the possible problems from the database
		DBAdapterScreening db = new DBAdapterScreening(getActivity());
		db.open();
		Cursor c = db.getAllItemProblems(ScreeningItemId);
		while (c.moveToNext()) {
			ItemProblemRow ip = new ItemProblemRow(c.getInt(0),
					ScreeningItemId, c.getInt(2), c.getString(3), c
							.getString(4).equals("T"), c.getInt(7) > 0, 0);
			if (ip.isChecked())
				ip.setScreeningProblemId(c.getInt(7));
			datalist.add(ip);
		}
		db.close();

		// get a list of all the parentId's
		List<Integer> tempParent = new ArrayList<Integer>();
		for (ItemProblemRow i : datalist) {
			if (!tempParent.contains(i.getParentId()) && i.getParentId() >= 0) {
				tempParent.add(i.getParentId());
			}
		}

		// check each item if it is another items parentId
		// if it is not, it is the and of a branch
		for (ItemProblemRow i : datalist) {
			if (!tempParent.contains(i.getId())) {
				i.setBranchEnd(true);
			}
		}

		// get all the parents to set the root list
		list = new ArrayList<ItemProblemRow>();
		for (ItemProblemRow i : datalist) {
			if (i.getParentId() == -1) {
				list.add(i);
			}
		}

		Log.i(TAG, "size: " + list.size());

		// load the detailview adapter
		SingleItemProblemRowAdapter adapter = new SingleItemProblemRowAdapter(
				getActivity(), ScreeningItemId, list);
		setListAdapter(adapter);

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {

		// get the selected item
		Log.i(TAG, "Clicked on item " + position);
		ItemProblemRow selected = (ItemProblemRow) l.getAdapter().getItem(
				position);

		// if the selected item is not the end of a branch the childs should be
		// loaded
		if (!selected.isBranchEnd()) {
			if (!selected.isSelected()) {
				for (ItemProblemRow i : datalist) {
					if (i.getParentId() == selected.getId()) {
						i.setNode(selected.getNode() + 1);
						list.add(position + 1, i);
					}
				}
			} else {
				// if the selected item is not at the end of the set all the
				// childs to not selected
				// and remove them from the adpater
				for (ItemProblemRow i : removeChilds(selected)) {
					i.setSelected(false);
					list.remove(i);
				}

			}
			selected.setSelected(!selected.isSelected());
		}

		SingleItemProblemRowAdapter adapter = new SingleItemProblemRowAdapter(
				getActivity(), ScreeningItemId, list);

		// update the adapter
		setListAdapter(adapter);
		adapter.notifyDataSetChanged();
		getListView().setSelection(position);
	}

	/**
	 * gets all the items that should be removed from the list
	 * 
	 * @param selected
	 * @return
	 */
	public Set<ItemProblemRow> removeChilds(ItemProblemRow selected) {
		Set<ItemProblemRow> temp = new HashSet<ItemProblemRow>();
		for (ItemProblemRow i : list) {
			if (i.getParentId() == selected.getId()) {
				if (!i.isBranchEnd()) {
					temp.addAll(removeChilds(i));
				}
				Log.i(TAG, "Succeeded: " + temp.add(i));
			}
		}
		return temp;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.activity_list, container,
				false);

		return rootView;

	}

}
